﻿using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using SO_DataFetcher.PubFunctions;
/*************************************************************************************
* CLR版本：       4.0.30319.269
* 类 名 称：      FrmDBLinkManager
* 机器名称：      
* 命名空间：      SO_DataFetcher.Forms
* 文 件 名：      FrmDBLinkManager.cs
* 创建时间：      2014/10/30
* 作    者：      Jay.zhang
* 功    能:
* 修改时间：
* 修 改 人：
*************************************************************************************/
using SO_DataFetcher.PubFunctions.DataLink;

namespace SO_DataFetcher.Forms
{
    public partial class FrmDbLinkManager : Form
    {
        public FrmDbLinkManager()
        {
            InitializeComponent();

            PubControls.InitialDataGridView(this.dataGridView1);
        }

        /// <summary>
        /// 当前操作的类型
        /// </summary>
        private string _type = string.Empty;

        /// <summary>
        /// 当前选中的Id
        /// </summary>
        private string _dataLinkId = string.Empty;

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            _type = "Add";
            _dataLinkId = string.Empty;
            ShowDataLink();
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_dataLinkId))
            {
                MessageBox.Show(@"没有选中任何数据，请先点击选中后再进行编辑");
                return;
            }

            _type = "Edit";

            ShowDataLink();
        }

        /// <summary>
        /// 操作单个DataLink
        /// </summary>
        private void ShowDataLink()
        {
            if (!string.IsNullOrEmpty(_type))
            {
                FrmDbLink frmDbLink = new FrmDbLink(_dataLinkId,this._type);
                if (frmDbLink.ShowDialog(this) == DialogResult.OK)
                {  
                    DataLinkStruct dataLink = frmDbLink.TempDataLink;
                    if (!string.IsNullOrEmpty(dataLink.Index))
                    {                                      
                        SaveDataLinks(dataLink);

                        this.dataGridView1.DataSource = PubDataLinks.DataTableDataLinks;
                    }
                }
            }
        }

        /// <summary>
        /// 更新内存结构和xml文件
        /// </summary>
        /// <param name="dataLink"></param>
        private void SaveDataLinks(DataLinkStruct dataLink)
        {
            string index = dataLink.Index;

            DataRow[] datarows = PubDataLinks.DataTableDataLinks.Select("Index='" + index + "'");

            if (datarows.Length > 0)
            {
                switch (_type)
                {
                    case "Edit":
                        DataRow dataRow = datarows[0];
                        dataRow["Index"] = dataLink.Index;
                        dataRow["Id"] = dataLink.Id;
                        dataRow["Ip"] = dataLink.Ip;
                        dataRow["Port"] = dataLink.Port;
                        dataRow["Name"] = dataLink.Name;
                        dataRow["Password"] = dataLink.Password;
                        dataRow["DataTable"] = dataLink.DataTable;
                        dataRow["Description"] = dataLink.Description;
                        dataRow["LinkType"] = dataLink.LinkType;
                        break;
                    case "Delete":
                        PubDataLinks.DataTableDataLinks.Rows.Remove(datarows[0]);
                        break;
                }
            }
            else
            {
                switch (_type)
                {
                    case "Add":
                        DataRow dataRow = PubDataLinks.DataTableDataLinks.NewRow();
                        dataRow["Index"] = dataLink.Index;
                        dataRow["Id"] = dataLink.Id;
                        dataRow["Ip"] = dataLink.Ip;
                        dataRow["Port"] = dataLink.Port;
                        dataRow["Name"] = dataLink.Name;
                        dataRow["Password"] = dataLink.Password;
                        dataRow["DataTable"] = dataLink.DataTable;
                        dataRow["Description"] = dataLink.Description;
                        dataRow["LinkType"] = dataLink.LinkType;
                        PubDataLinks.DataTableDataLinks.Rows.Add(dataRow);
                        break;
                }
            }

            PubDataLinks.SaveDataSetLinks();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmDbLinkManager_Load(object sender, EventArgs e)
        {
            if (PubDataLinks.DataTableDataLinks != null)
            {
                this.dataGridView1.DataSource = PubDataLinks.DataTableDataLinks;
            }
        }

        /// <summary>
        /// 判断单击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            var dataGridViewRow = this.dataGridView1.CurrentRow;
            if (dataGridViewRow != null)
            {
                string id = dataGridViewRow.Cells[1].Value.ToString();
                if (!string.IsNullOrEmpty(id))
                {
                    _dataLinkId = id;
                }
            }
           
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDel_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_dataLinkId))
            {
                MessageBox.Show(@"没有选中任何数据，请选中表格中的项之后再操作！");
                return;
            }
            _type = "Delete";
            
            SaveDataLinks(PubDataLinks.DataLinks[_dataLinkId]);
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                return;
            }

            if (e.ColumnIndex == 0)
            {
                this._dataLinkId = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                _type = "Edit";
                ShowDataLink();
            }
        }
    }
}
